package com.hzlh.service;


import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzlh.common.util.DateUtil;
import com.hzlh.domain.po.IPhoneStartPo;
import com.hzlh.domain.po.PhoneUserPo;
import com.hzlh.domain.po.UserReportPo;
import com.hzlh.mapper.LoginMapper;
import com.hzlh.mapper.UserReportMapper;

@Service
public class UserReportService {
	
	@Autowired
	private UserReportMapper userReportMapper;
	@Autowired
	private LoginMapper loginMapper;
	/**
	 * 按小时统计某一天的注册用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserReportByDayList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report.setStartTime(DateUtil.getCurrDate(new Date())+ "00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ "23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		return userReportMapper.getUserRegisterByDayList(report);
	}
	/**
	 * 按天统计注册用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserReportByHourList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		
		return userReportMapper.getUserRegisterByHourList(report);	
	}
	/**
	 * 按月统计注册用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserReportByMonthList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		
		return userReportMapper.getUserRegisterByMonthList(report);	
	}
	
	/**
	 * 按小时统计某一天的激活用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserActiveByDayList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report.setStartTime(DateUtil.getCurrDate(new Date())+ "00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ "23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		return userReportMapper.getUserActiveByDayList(report);
	}
	/**
	 * 按天统计激活用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserActiveByHourList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		return userReportMapper.getUserActiveByHourList(report);	
	}
	/**
	 * 按月统计激活用户数
	 * @param report
	 * @return
	 */
	public List<UserReportPo> getUserActiveByMonthList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		return userReportMapper.getUserActiveByMonthList(report);	
	}
	
	/**
	 * 按时间段统计注册用户总数
	 * @param report
	 * @return
	 */
	public int getUserCountByTimeList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		
		return userReportMapper.getUserCountByTimeList(report);	
	}
	
	public int getUserActiveCountByTimeList(UserReportPo report){
		if(report.getStartTime()==null || report.getEndTime()==null){
			report=new UserReportPo();
			report.setStartTime(DateUtil.getCurrDate(new Date())+ " 00:00:00");
			report.setEndTime(DateUtil.getCurrDate(new Date())+ " 23:59:59");
		}else{
			report.setStartTime(report.getStartTime()+" 00:00:00");
			report.setEndTime(report.getEndTime()+" 23:59:59");
		}
		
		return userReportMapper.getUserActiveCountByTimeList(report);	
	}
	
	public void exportActiveData(String startTime,String endTime,String nameType,HttpServletResponse response,HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("nameType", nameType);
		List<IPhoneStartPo> list = null;
		list = loginMapper.getUserActivePage(map);
		if(list.size() > 0){
			for (int i = 0; i < list.size(); i++) {
				String lists = list.get(i).getUuId();
				if(null == lists || "".equals(lists)){
					list.get(i).setUuId("--");
				}else{
					String uuid ="****" +  lists.substring(3, 7)
							+ "****" ;
					list.get(i).setUuId(uuid);
				}
			}
		}
		try {
			exportActiveXls(request, response, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出激活用户xls
	 */
	public void exportActiveXls(HttpServletRequest req, HttpServletResponse response,
			List<IPhoneStartPo> list) throws Exception {
		OutputStream os = null;
		WritableWorkbook wbook = null;
		int row = 3; //索引-从0开始
		String fileName = "激活用户名单_";
		String titName = "激活用户名单_";
		String agent = req.getHeader("User-Agent");
		boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
		try {
			if (isMSIE) {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} else {
				fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			}
			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			String title = fileName + DateUtil.getCurrDate(new Date()); // 标题
			response.setHeader("Content-disposition", "attachment; filename="
					+ title + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			wbook = Workbook.createWorkbook(os); // 建立excel文件
			WritableSheet wsheet = wbook.createSheet(
					titName + DateUtil.getCurrDate(new Date()), 0); // sheet名称
			// 设置excel标题
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			WritableCellFormat wcfFC = new WritableCellFormat(wfont);
			wcfFC.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色  
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE); // 设置单元格居中
			wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框  
			wsheet.setColumnView(0, 20);
			wsheet.setColumnView(1, 20);
			wsheet.setColumnView(2, 20);
			wsheet.setColumnView(3, 20);
			wsheet.addCell(new Label(0, 0, titName
					+ DateUtil.getCurrDate(new Date()), wcfFC));
			wsheet.mergeCells(0, 0, row, 0);//合并单元格
			wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			wcfFC = new WritableCellFormat(wfont);
			jxl.write.NumberFormat df = new jxl.write.NumberFormat("#,##0.00");
			jxl.write.WritableCellFormat wcfD = new jxl.write.WritableCellFormat(
					df);// 小数
			wcfD.setWrap(true);
			wcfD.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfD.setAlignment(jxl.format.Alignment.RIGHT);
			wcfD.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0");
			jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(
					nf);// 整数
			jxl.write.WritableCellFormat wcfS = new jxl.write.WritableCellFormat();// 字符串
			wcfS.setWrap(true);
			wcfS.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfS.setAlignment(jxl.format.Alignment.LEFT);
			wcfS.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			wcfN.setWrap(true);
			wcfN.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfN.setAlignment(jxl.format.Alignment.RIGHT);
			wcfN.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			// 开始生成主体内容
			wsheet.addCell(new Label(0, 2, "序号", wcfS));
			wsheet.addCell(new Label(1, 2, "用户ID", wcfS));
			wsheet.addCell(new Label(2, 2, "用户类型", wcfS));
			wsheet.addCell(new Label(3, 2, "访问时间", wcfS));
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					IPhoneStartPo up = list.get(i);
					wsheet.addCell(new Label(0, i + 3, String.valueOf(up.getId()), wcfS));
					wsheet.addCell(new Label(1, i + 3, up.getUuId(), wcfS));
					wsheet.addCell(new Label(2, i + 3, String.valueOf(up.getType() == 2 ? "--" : (up.getType() == 0 ? "IOS" : "安卓")), wcfS));
					wsheet.addCell(new Label(3, i + 3, String.valueOf(up
							.getCreateTime() == null ? "--" : DateUtil
							.dateStr(up.getCreateTime())), wcfS));
				}
			}
			// 主体内容生成结束
			wbook.write(); // 写入文件
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			wbook.close();
			os.close(); // 关闭流
		}
	}
	
	public void exportRegisterData(String startTime,String endTime,HttpServletResponse response,HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		List<PhoneUserPo> list = null;
		list = loginMapper.getUserRegisterPage(map);
		try {
			exportRegisterXls(request, response, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出注册用户xls
	 */
	public void exportRegisterXls(HttpServletRequest req, HttpServletResponse response,
			List<PhoneUserPo> list) throws Exception {
		OutputStream os = null;
		WritableWorkbook wbook = null;
		int row = 3; //索引-从0开始
		String fileName = "注册用户名单_";
		String titName = "注册用户名单_";
		String agent = req.getHeader("User-Agent");
		boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
		try {
			if (isMSIE) {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} else {
				fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			}
			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			String title = fileName + DateUtil.getCurrDate(new Date()); // 标题
			response.setHeader("Content-disposition", "attachment; filename="
					+ title + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			wbook = Workbook.createWorkbook(os); // 建立excel文件
			WritableSheet wsheet = wbook.createSheet(
					titName + DateUtil.getCurrDate(new Date()), 0); // sheet名称
			// 设置excel标题
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			WritableCellFormat wcfFC = new WritableCellFormat(wfont);
			wcfFC.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色  
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE); // 设置单元格居中
			wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框  
			wsheet.setColumnView(0, 20);
			wsheet.setColumnView(1, 20);
			wsheet.setColumnView(2, 20);
			wsheet.setColumnView(3, 20);
			wsheet.setColumnView(4, 20);
			wsheet.setColumnView(5, 20);
			wsheet.setColumnView(6, 20);
			wsheet.setColumnView(7, 20);
			wsheet.addCell(new Label(0, 0, titName
					+ DateUtil.getCurrDate(new Date()), wcfFC));
			wsheet.mergeCells(0, 0, row, 0);//合并单元格
			wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			wcfFC = new WritableCellFormat(wfont);
			jxl.write.NumberFormat df = new jxl.write.NumberFormat("#,##0.00");
			jxl.write.WritableCellFormat wcfD = new jxl.write.WritableCellFormat(
					df);// 小数
			wcfD.setWrap(true);
			wcfD.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfD.setAlignment(jxl.format.Alignment.RIGHT);
			wcfD.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#,##0");
			jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(
					nf);// 整数
			jxl.write.WritableCellFormat wcfS = new jxl.write.WritableCellFormat();// 字符串
			wcfS.setWrap(true);
			wcfS.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfS.setAlignment(jxl.format.Alignment.LEFT);
			wcfS.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			wcfN.setWrap(true);
			wcfN.setBorder(jxl.format.Border.ALL,
					jxl.format.BorderLineStyle.THIN);
			wcfN.setAlignment(jxl.format.Alignment.RIGHT);
			wcfN.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			// 开始生成主体内容
			wsheet.addCell(new Label(0, 2, "编号", wcfS));
			wsheet.addCell(new Label(1, 2, "手机号码", wcfS));
			wsheet.addCell(new Label(2, 2, "昵称", wcfS));
			wsheet.addCell(new Label(3, 2, "姓名", wcfS));
			wsheet.addCell(new Label(4, 2, "性别", wcfS));
			wsheet.addCell(new Label(5, 2, "生日", wcfS));
			wsheet.addCell(new Label(6, 2, "地址", wcfS));
			wsheet.addCell(new Label(7, 2, "注册时间", wcfS));
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					PhoneUserPo up = list.get(i);
					wsheet.addCell(new Label(0, i + 3, String.valueOf(up.getId()), wcfS));
					wsheet.addCell(new Label(1, i + 3, up.getPhone(), wcfS));
					wsheet.addCell(new Label(2, i + 3, up.getNickName() == null ? "--" : up.getNickName(), wcfS));
					wsheet.addCell(new Label(3, i + 3, up.getName(), wcfS));
					wsheet.addCell(new Label(4, i + 3, String.valueOf(up.getSex() == null ? "--" : (up.getSex() == 1 ? "女" : "男")), wcfS));
					wsheet.addCell(new Label(5, i + 3, up.getBirthday() == null ? "--" : up.getBirthday(), wcfS));
					wsheet.addCell(new Label(6, i + 3, up.getAddress() == null ? "--" : up.getAddress(), wcfS));
					wsheet.addCell(new Label(7, i + 3, String.valueOf(up
							.getCreateTime() == null ? "--" : DateUtil
							.dateStr(up.getCreateTime())), wcfS));
				}
			}
			// 主体内容生成结束
			wbook.write(); // 写入文件
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			wbook.close();
			os.close(); // 关闭流
		}
	}
}